import * as path from "path";
import * as useUrl from "url";

export class useURL {
	constructor(public url: string) {}
	isCDN() {
		if (
			this.url.startsWith("https://") ||
			this.url.startsWith("http://") ||
			this.url.startsWith("//")
		) {
			return true;
		}
		return false;
	}

	pure() {
		const url = useUrl.parse(this.url);
		if (url.search && this.url) {
			return this.url.replace(url.search, "");
		}
		return this.url;
	}

	getUrl(address: string) {
		return this.isCDN()
			? this.pure()
			: this.url.startsWith("/")
			? path
					.join(
						useUrl.parse(address).protocol + "//" + useUrl.parse(address).host,
						this.pure()
					)
					.replace(/\\/g, "/")
			: path.join(address, this.pure()).replace(/\\/g, "/");
	}

	getFile(publicDir: string) {
		return this.isCDN() ? "/" : path.join(publicDir, this.pure());
	}
}
